package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.ScClaTimeMapper;
import com.ruoyi.system.mapper.ScStudentCourseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ClaTimeAttendMapper;
import com.ruoyi.system.service.IClaTimeAttendService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 上课出勤Service业务层处理
 * 
 * @author ZHANGQIBO
 * @date 2024-01-21
 */
@Service
public class ClaTimeAttendServiceImpl implements IClaTimeAttendService 
{
    @Autowired
    private ClaTimeAttendMapper claTimeAttendMapper;

    @Autowired
    private ScClaTimeMapper scClaTimeMapper;
    @Autowired
    private ScStudentCourseMapper scStudentCourseMapper;

    /**
     * 查询上课出勤
     * 
     * @param attendId 上课出勤主键
     * @return 上课出勤
     */
    @Override
    public ClaTimeAttend selectClaTimeAttendByAttendId(Long attendId)
    {
        return claTimeAttendMapper.selectClaTimeAttendByAttendId(attendId);
    }

    /**
     * 查询上课出勤列表
     * 
     * @param claTimeAttend 上课出勤
     * @return 上课出勤
     */
    @Override
    public List<StudentTime> selectClaTimeAttendList(ClaTimeAttend claTimeAttend)
    {
        return claTimeAttendMapper.selectClaTimeAttendList(claTimeAttend);
    }
    @Override
    public List<StudentTime> selectClaTimeAttendListAPP(ClaTimeAttend claTimeAttend)
    {
        return claTimeAttendMapper.selectClaTimeAttendListAPP(claTimeAttend);
    }

    /**
     * 新增上课出勤
     * 
     * @param claTimeAttend 上课出勤
     * @return 结果
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int insertClaTimeAttend(ClaTimeAttend claTimeAttend)
    {
        claTimeAttend.setCreateUser(SecurityUtils.getUsername());
        claTimeAttend.setCreateTime(DateUtils.getNowDate());
        ScClaTime scClaTime = new ScClaTime();
        Long courseTimeId = claTimeAttend.getCourseTimeId();
        scClaTime.setCourseTimeId(courseTimeId);
        scClaTime.setStatus("2");

        scClaTimeMapper.updateScClaTime(scClaTime);
        List<ClaTimeAttendItem> claTimeAttendItemList = claTimeAttend.getClaTimeAttendItemList();
        for (ClaTimeAttendItem cla:claTimeAttendItemList
             ) {
            ScStudentCourse scStudentCourse = new ScStudentCourse();
            scStudentCourse.setStudentCourseId(cla.getStudentCourseId());
            scStudentCourse.setBalanceHour(cla.getBalanceHour());
            claTimeAttend.setStudentId(cla.getStudentId()!=null?cla.getStudentId():null);
            claTimeAttend.setStudentCourseId(cla.getStudentCourseId()!=null?cla.getStudentCourseId():null);
            claTimeAttend.setPayHour(cla.getStuLoseHour()!=null?cla.getStuLoseHour():null);
            claTimeAttend.setAttendStatus(cla.getAttendStatus()!=null?cla.getAttendStatus():null);
           if(claTimeAttendMapper.insertClaTimeAttend(claTimeAttend)<0 ||
                   scStudentCourseMapper.updateScStudentCourse(scStudentCourse)<0)
               return -1;
        }

        return 1;

    }

    /**
     * 修改上课出勤
     * 
     * @param claTimeAttend 上课出勤
     * @return 结果
     */
    @Override
    public int updateClaTimeAttend(ClaTimeAttend claTimeAttend)
    {
        claTimeAttend.setCreateUser(SecurityUtils.getUsername());
        claTimeAttend.setCreateTime(DateUtils.getNowDate());
        ScClaTime scClaTime = new ScClaTime();
        Long courseTimeId = claTimeAttend.getCourseTimeId();
        scClaTime.setCourseTimeId(courseTimeId);
        List<ClaTimeAttendItem> claTimeAttendItemList = claTimeAttend.getClaTimeAttendItemList();
        for (ClaTimeAttendItem cla:claTimeAttendItemList
        ) {
            claTimeAttend.setStudentId(cla.getStudentId()!=null?cla.getStudentId():null);
            claTimeAttend.setStudentCourseId(cla.getStudentCourseId()!=null?cla.getStudentCourseId():null);
            claTimeAttend.setPayHour(cla.getStuLoseHour()!=null?cla.getStuLoseHour():null);
            claTimeAttend.setAttendStatus(cla.getAttendStatus()!=null?cla.getAttendStatus():null);
            ClaTimeAttend claTimeAttend1 = claTimeAttendMapper.selectClaTimeAttendItem(claTimeAttend);


            ScStudentCourse scStudentCourse = new ScStudentCourse();
            scStudentCourse.setStudentCourseId(cla.getStudentCourseId());
            scStudentCourse.setBalanceHour(cla.getBalanceHour().add(claTimeAttend1.getPayHour()));




            if(claTimeAttendMapper.updateClaTimeAttend(claTimeAttend)<0 ||
                    scStudentCourseMapper.updateScStudentCourse(scStudentCourse)<0)
                return -1;
        }

        return 1;
    }

    @Override
    public int insertQuanClaTimeAttend(ClaTimeAttend claTimeAttend) {
        claTimeAttend.setCreateUser(SecurityUtils.getUsername());
        claTimeAttend.setCreateTime(DateUtils.getNowDate());
        ScClaTime scClaTime = new ScClaTime();
        Long courseTimeId = claTimeAttend.getCourseTimeId();
        scClaTime.setCourseTimeId(courseTimeId);
        scClaTime.setStatus("2");

        scClaTimeMapper.updateScClaTime(scClaTime);
        List<ClaTimeAttendItem> claTimeAttendItemList = claTimeAttend.getClaTimeAttendItemList();
        for (ClaTimeAttendItem cla:claTimeAttendItemList
        ) {


            if(cla.getAttendStatus().equals("1")){
                ScStudentCourse scStudentCourse = new ScStudentCourse();
                scStudentCourse.setStudentCourseId(cla.getStudentCourseId());
                scStudentCourse.setBalanceHour(cla.getBalanceHour().subtract(claTimeAttend.getPayHour()));
                if(scStudentCourseMapper.updateScStudentCourse(scStudentCourse)<0)
                    return -1;
            }


            claTimeAttend.setStudentId(cla.getStudentId()!=null?cla.getStudentId():null);
            claTimeAttend.setStudentCourseId(cla.getStudentCourseId()!=null?cla.getStudentCourseId():null);
            claTimeAttend.setAttendStatus(cla.getAttendStatus()!=null?cla.getAttendStatus():null);
            if(claTimeAttendMapper.insertClaTimeAttend(claTimeAttend)<0)
                return -1;
        }

        return 1;
    }

    @Override
    public int updateQuanClaTimeAttend(UpdateAttend updateAttend) {
        ClaTimeAttend claTimeAttend = new ClaTimeAttend();
        claTimeAttend.setAttendId(updateAttend.getAttendId());
        claTimeAttend.setAttendStatus(updateAttend.getAttendStatus());
        if(updateAttend.getAttendStatus().equals("1")) {

            claTimeAttend.setPayHour(updateAttend.getPayHour());
            ScStudentCourse scStudentCourse = new ScStudentCourse();
            scStudentCourse.setStudentCourseId(updateAttend.getStudentCourseId());
            scStudentCourse.setBalanceHour(updateAttend.getBalanceHour().subtract(updateAttend.getPayHour()));
            if (scStudentCourseMapper.updateScStudentCourse(scStudentCourse) < 0)
                return -1;
        }
        else if (updateAttend.getAttendStatus().equals("2")){
            claTimeAttend.setPayHour(BigDecimal.valueOf(0));
            ScStudentCourse scStudentCourse = new ScStudentCourse();
            scStudentCourse.setStudentCourseId(updateAttend.getStudentCourseId());
            scStudentCourse.setBalanceHour(updateAttend.getBalanceHour().add(updateAttend.getPayHour()));
            if (scStudentCourseMapper.updateScStudentCourse(scStudentCourse) < 0)
                return -1;
        }


        return  claTimeAttendMapper.updateUpdateAttend(claTimeAttend);
    }

    /**
     * 批量删除上课出勤
     * 
     * @param attendIds 需要删除的上课出勤主键
     * @return 结果
     */
    @Override
    public int deleteClaTimeAttendByAttendIds(Long[] attendIds)
    {
        return claTimeAttendMapper.deleteClaTimeAttendByAttendIds(attendIds);
    }

    /**
     * 删除上课出勤信息
     * 
     * @param attendId 上课出勤主键
     * @return 结果
     */
    @Override
    public int deleteClaTimeAttendByAttendId(Long attendId)
    {
        return claTimeAttendMapper.deleteClaTimeAttendByAttendId(attendId);
    }
}
